﻿/****************************************************************************
Copyright (c) 2014-2015 凌惊雪 微信:Lingjingxue 邮箱:lingjingxue@sina.com QQ:271487457

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/

using System;
using System.Collections.Generic;
using System.Net.Sockets;
using System.Text;
using ServerBase.Service;
using ServerBase.Client;

namespace ServerBase.Protocol
{
    //协议扩展
    public static partial class ProtocolExtensions
    {
        #region 扩展
        //失败信息
        public static void MessageFail(this ProtocolMsgBase rsp, string msg)
        {
            rsp.Result = Config.EProtocolResult.失败信息;
            rsp.Shuttle = msg;
        }
        #endregion

        #region 时间日期
        public static string ToStr(this DateTime dt)
        {
            switch (BaseServerInfo.Language)
            {
                case Config.ELanguage.中文简体:
                    return dt.ToString("yyyy年MM月dd日 HH时mm分ss秒");
                case Config.ELanguage.韩语:
                    return dt.ToString("yyyy년MM월dd일 HH시mm분ss초");
                default:
                    break;
            }
            return dt.ToString();
        }
        public static string ToStr(this TimeSpan ts, int format = 0)
        {
            switch (format)
            {
                case 1:
                    return ts.ToString(@"hh\:mm\:ss");
                case 2:
                    return $"{ts.TotalSeconds}";
                case 3:
                    return ts.ToString("G");
                case 4:
                    return $"{(int)Math.Ceiling(ts.TotalDays)}";
                default:
                    return ts.ToString();
            }
        }
        #endregion

        #region 奖励项
        //复制奖励项
        public static CLS_AwardItem Clone(this CLS_AwardItem Award)
        {
            return new CLS_AwardItem()
            {
                Type = Award.Type,
                ConfigId = Award.ConfigId,
                Count = Award.Count,
                Level = Award.Level,
            };
        }
        //生成奖励项
        public static CLS_AwardItem ToAward(this List<int> AwardText)
        {
            var Award = new CLS_AwardItem();
            if (AwardText.Count > 0)
            {
                Award.Type = (short)AwardText[0];
                if (AwardText.Count > 1)
                {
                    Award.ConfigId = AwardText[1];
                    if (AwardText.Count > 2)
                    {
                        Award.Count = AwardText[2];
                        if (AwardText.Count > 3)
                        {
                            Award.Level = AwardText[3];
                        }
                    }
                }
            }
            Award.Count = Math.Max(Award.Count, 1);
            return Award;
        }
        //生成奖励项列表
        public static void ToListAward(this List<List<int>> ListAwardText, ref List<CLS_AwardItem> ListAward)
        {
            foreach (var AwardText in ListAwardText)
            {
                if (AwardText[0] == 0)
                {
                    continue;
                }
                var Award = AwardText.ToAward();
                ListAward.Add(Award);
            }
        }

        //生成奖励项
        public static void ToAwardDrop(this List<int> AwardText, out int percent, out CLS_AwardItem Award)
        {
            percent = 0;
            Award = new CLS_AwardItem();
            if (AwardText.Count > 0)
            {
                percent = AwardText[0];
                if (AwardText.Count > 1)
                {
                    Award.Type = (short)AwardText[1];
                    if (AwardText.Count > 2)
                    {
                        Award.ConfigId = AwardText[2];
                        if (AwardText.Count > 3)
                        {
                            Award.Count = AwardText[3];
                            if (AwardText.Count > 4)
                            {
                                Award.Level = AwardText[4];
                            }
                        }
                    }
                }
            }
            Award.Count = Math.Max(Award.Count, 1);
        }
        //生成奖励项词典
        public static Dictionary<CLS_AwardItem, int> ToDictAwardDrop(this List<List<int>> ListListText)
        {
            Dictionary<CLS_AwardItem, int> DictAwardDrop = new Dictionary<CLS_AwardItem, int>();
            foreach (var AwardText in ListListText)
            {
                AwardText.ToAwardDrop(out var percent, out var Award);
                DictAwardDrop.Add(Award, percent);
            }
            return DictAwardDrop;
        }
        #endregion

    }


}
